package tack09;

import java.util.Scanner;

public class Method {
    private User[] users = {
            new User("黄森", "123456", "123456", 1200),
            new User("盖茨", "888888", "888888", 99999),
            new User("老马", "666666", "666666", 98999),
            new User("乞丐King","987456","654789",100000),
    };
    public int index;
    //方法类
    public Scanner inout = new Scanner(System.in);
    //流程
    public void process(){
        System.out.println("~~~~~~~~~~欢迎来到银行~~~~~~~~~~~");
        while (true){
            login();
        }

    }
    //登录
    public void login(){
        for(int i = 0 ; i < 3 ; i ++){
            System.out.println("请输入账户");
            String account = inout.next();
            System.out.println("请输入密码");
            String pwd = inout.next();
            for(int j = 0 ; j < users.length ; j ++) {
                if (users[j].number.equals(account) && users[j].password.equals(pwd)) {
                    System.out.println("登录成功");
                    index = j;
                    menu();
                    return;
                }
            }
            System.out.println("登录失败你还有" + ( 2 - i )+"次机会");
        }
        System.out.println("登录失败以吞卡请联系银行");
    }
    //菜单
    public void menu(){
        while (true){
    System.out.println("请输入你要办理的业务");
    System.out.println("1 存款  2.取钱  3.查询余额  4.转账 5.切换转户 6.更改密码 7.退出");
    int a = inout.nextInt();
    switch (a) {
        case 1:
            save();
            break;
        case 2:
            get();
            break;
        case 3:
            search();
            break;
        case 4:
            transfer();
            break;
        case 5:
            login();
            break;
        case 6:
            amend();
            break;
        default:
            System.out.println("输入有误重新输入");
            menu();
            break;
    }
    }
    }
    //小菜单
    public void smallMenu(){
        System.out.println("1 返回菜单  2 切换用户  3 退出");
        int a = inout.nextInt();
        if( a == 1 ){
            menu();
        }
        if ( a == 2){
            login();
        }
        if( a == 3){
            System.out.println("感谢使用银行系统正在退出");
            System.exit(0);
        }
    }
    //存钱
    public void save(){
        System.out.println("请输入你要存的的金额");
        double money1 = inout.nextDouble();
        users[index].money += money1;
        System.out.println("你存入了" + money1 +"元   账户余额" + users[index].money);
        smallMenu();

    }
    //取钱
    public void get(){
        System.out.println("请输入你要取走的金额");
        double money2 = inout.nextDouble();
        if(money2 > users[index].money){
            System.out.println("你没有那么多钱请重新输入");
            get();
        }else {
            users[index].money = users[index].money - money2;
            System.out.println("你取走了" + money2 + "元    还剩"+users[index].money +" 元");
            smallMenu();
        }
    }
    //查询
    public void search(){
        System.out.println("你的账户还有" + users[index].money);
    }
    //转账
    public void transfer(){
        System.out.println("请输入你要转入的账户");
        String number = inout.next();
        for(int i = 0 ; i < users.length ; i ++){
            if( number == users[i].number){
                System.out.println("请输入你转账的金额");
                double money = inout.nextDouble();
                if(users[index].money > money){
                    System.out.println("转账成功");
                    users[index].money -= money;
                    System.out.println("余额为" + users[index].money);
                    smallMenu();
                }else{
                    System.out.println("余额不足");
                    smallMenu();
                }
            }
        }
        System.out.println("你输入的转户不正确请重新输入？");
        transfer();
    }
    //修改密码
    public void amend(){
        System.out.println("请输入原密码：");
        String oldPwd = inout.next();
        if(!oldPwd.equals(users[index].number)){
            System.out.println("原密码输入错误");
            return;
        }
        System.out.println("请输入新密码：");
        String newPwd = inout.next();
        if(oldPwd.equals(newPwd)){
            System.out.println("新密码不能和原密码一样");
            return;
        }
        System.out.println("请再次输入新密码：");
        String confirmPwd = inout.next();
        if(!newPwd.equals(confirmPwd)){
            System.out.println("两次密码不一致");
            return;
        }
        users[index].number = newPwd;
        System.out.println("密码修改成功");
        return;

    }

    public static void main(String[] args) {
        Method method = new Method();
        method.process();



    }

}
